﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InfoSource.Models.Views;
using InfoSource.Models.objects;
using InfoSource.Data;
using InfoSource.Utilities;

namespace InfoSource.Controllers
{
    public class AppointmentRemindersController : BaseController
    {
        //
        // GET: /AppointmentReminders/

        public ActionResult Index()
        {
            return View();
        }

        /*--------------------------------------------------------------------*/
        public ActionResult EditTemplates()
        {
            AppointmentReminders_EditTemplateViewModel model = new AppointmentReminders_EditTemplateViewModel();

            return DynamicView(model);
        }

		/*--------------------------------------------------------------------*/
		public ActionResult SendIndividualAppointmentReminder()
		{
			AppointmentReminders_SendIndividualAppointmentReminderViewModel model = new AppointmentReminders_SendIndividualAppointmentReminderViewModel();

			return DynamicView(model);
		}


        /*--------------------------------------------------------------------*/
        [ValidateInput(false)]
        public JsonResult SaveTemplate(int? id, string templateName, string templateBody)
        {
            if (string.IsNullOrEmpty(templateName))
            {
                return Json(new DBResult() { StatusCode = -1, StatusMsg = "template name cannot be empty." });
            }
            bool isNew = (!id.HasValue);
            DBResult result = AppointmentRemindersRequests.SaveTemplate(id, templateName, templateBody);
            result.AdditionalData.Add("IsNew", isNew);

            return Json(result);
        }

        /*--------------------------------------------------------------------*/
        public JsonResult DeleteTemplate(int? id)
        {
            DBResult result = new DBResult();
            if (!id.HasValue || id == 0)
            {
                result.StatusCode = -1;
                result.StatusMsg = "invalid or missing id";

                return Json(result);
            }


            result = AppointmentRemindersRequests.DeleteTemplate((int)id);

            return Json(result);
        }

        /*--------------------------------------------------------------------*/
        [ValidateInput(false)]
        public JsonResult SendTestEmail(string to, string content)
        {
            DBResult result = new DBResult();

            result.StatusCode = Helpers.SendEmail(to.Split(';'), "Test Email", content);

            result.StatusMsg = (result.StatusCode == 1 ? "success" : "fail");

            return Json(result);
        }

		/*--------------------------------------------------------------------*/
		public JsonResult DoAccountSearch(string searchType, string searchString)
		{
			DBResult result = new DBResult();

			result = AppointmentRemindersRequests.SearchAccounts(searchType, searchString);

			return Json(result);
		}

		/*--------------------------------------------------------------------*/
		public JsonResult GetContractAppliances(int contractID)
		{
			DBResult result = new DBResult();

			result = AppointmentRemindersRequests.SearchAccounts(contractID);

			return Json(result);
		}

		/*--------------------------------------------------------------------*/
		public JsonResult PreviewTemplate(int contractID, int applianceID, int templateID)
		{
			DBResult result = new DBResult();

			AppointmentReminders_SendIndividualAppointmentReminderViewModel model = new AppointmentReminders_SendIndividualAppointmentReminderViewModel();
			string templateBody = model.PreviewTemplate(contractID, applianceID, templateID);

			result.StatusCode = 1;
			result.StatusMsg = "sucess";
			result.AdditionalData.Add("templateBody", templateBody);

			return Json(result);
		}
    }
}
